<?php
/**
*	html
*/
namespace Rainrock\Framework\kernel\Html;


use Rainrock\Framework\kernel\base\Base;
use Rainrock\Framework\kernel\base\Color;
use Rainrock\Framework\kernel\base\Web;

class HtmlBase{
	
	public static function get($mode='')
	{
		return new HtmlBase($mode);
	}
	
	private $title='',$bodyt = array(),$bodyf = array(),$bodystyle='',$style=array(),$jsarr = array(),$cssarr=array(),$script=array(),$jsarre=array(),$mdivobj = null,$viewobj = null,$ishei=false;
	
	
	public function __construct($mode='')
	{
		$color 	= APPTHEME;
		$cola	= Color::colorTorgb($color);
		$bgcol  = 'rgba('.$cola[0].','.$cola[1].','.$cola[2].',0.03)';
		$ismobile = Web::isMobile() ? 1 : 0;
		$this->addJs('jquery');
		$this->addJs('rock', time());
		if(Web::appType())$this->addJs('rockapp', time());
		$this->addJs('base64_min');
		$this->addJse('base');
		$this->addCss('rock');
		$this->addScript('maincolor="'.$color.'";urlstatic="'.URLSTATIC.'";ismobile='.$ismobile.';');
		$this->addStyle('body{--main-color:'.$color.';background:'.$bgcol.';--rgb-r:'.$cola[0].';--rgb-g:'.$cola[1].';--rgb-b:'.$cola[2].';}');
		if(Web::isMobile())$this->addStyle('body{--font-size:18px}');
		
		//暗黑模式的
		$ishei	= false;
		if($color == '#333333' || $color=='#555555')$ishei = true;
		if($ishei){
			$bgcol  	= 'rgba('.$cola[0].','.$cola[1].','.$cola[2].',0.9)';
			$bgcolor  	= 'rgba('.$cola[0].','.$cola[1].','.$cola[2].',0.5)';
			$this->addStyle('body{background:'.$bgcol.';color:white;--main-bgcolor:'.$bgcolor.'}');
			$this->addStyle('a:link,a:visited{color:white;}');
			$this->addStyle('.input,.textarea{background:'.$bgcol.';color:white}');
			$this->addStyle('.input:focus,.textarea:focus{border:.5px rgba(255,255,255,0.1) solid}');
			$this->addStyle('.zhu{color:white}');
			$this->addStyle('.webbtn:disabled{background:var(--main-bgcolor);color:#555555}');
			$this->addStyle('.webbtn-default,.webbtn-default:link,.webbtn-default:visited{background:var(--main-color);color:white;border:none;}');
			$this->ishei = true;
		}
	}
	
	/**
	*	是否黑色主题
	*/
	public function isBlack()
	{
		return $this->ishei;
	}
	
	public function setTitle($str)
	{
		$this->title = $str;
		return $this;
	}
	
	public function addBody($str)
	{
		if(!$this->mdivobj){
			$this->mdivobj = HtmlLabel::get();
			$this->mdivobj->addCont(PHP_EOL);
			$this->mdivobj->setId('mainbody');
			$this->mdivobj->setAlign('center');
		}
		$this->mdivobj->addCont($str);
		return $this;
	}
	
	public function addCont($str)
	{
		return $this->addBody($str);
	}
	
	/**
	*	添加顶部内容
	*/
	public function addTBody($str)
	{
		if(gettype($str)=='object')$str = $str->show();
		$this->bodyt[] = $str;
		return $this;
	}
	
	/**
	*	添加底部内容
	*/
	public function addFBody($str)
	{
		if(gettype($str)=='object')$str = $str->show();
		$this->bodyf[] = $str;
		return $this;
	}
	
	public function addJs($str,$lx='')
	{
		$url 	= ''.URLSTATIC.'webstatic/'.$str.'.js';
		if($lx)$url .= '?'.$lx.'';
		$this->jsarr[] = $url;
		return $this;
	}
	
	public function addJse($str, $jsy=false)
	{
		if(!$str)return $this;
		if(!$jsy)$str = ''.URLSTATIC.'webstatic/plugin/jsplugin_'.$str.'.js?'.time().'';
		$this->jsarre[] = $str;
		return $this;
	}
	
	public function addCss($str)
	{
		$this->cssarr[] = ''.URLSTATIC.'webstatic/'.$str.'.css?'.time().'';
		return $this;
	}
	
	public function addStyle($str)
	{
		$this->style[] = $str;
		return $this;
	}
	
	/**
	*	加js
	*/
	public function addScript($str)
	{
		$this->script[] = $str;
		return $this;
	}
	
	/**
	*	加主div样式
	*/
	public function addDstyle($key, $val)
	{
		return $this->mdivobj->addStyle($key, $val);
	}
	
	/**
	*	加主div样式
	*/
	public function addDattrs($key, $val)
	{
		return $this->mdivobj->addAttrs($key, $val);
	}
	
	/**
	*	获取主div
	*/
	public function getMdiv()
	{
		return $this->mdivobj;
	}
	
	/**
	*	对齐方式
	*/
	public function setAlign($str)
	{
		$this->mdivobj->setAlign($str);
		return $this;
	}
	
	public function addBodyStyle($key, $val)
	{
		$this->bodystyle.=''.$key.':'.$val.';';
		return $this;
	}
	
	/**
	*	添加引入模版
	*/
	public function setView($view)
	{
		$this->viewobj = $view;
		return $this;
	}
	
	
	public function show()
	{
		$str[] = '<!DOCTYPE html>';
		$str[] = '<html lang="zh-CN">';
		$str[] = '<head>';
		$str[] = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
		$str[] = '<meta name="viewport" content="maximum-scale=2.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>';
		$str[] = '<title>'.$this->title.'</title>';
		$logo  = 'images/logo.png';
		if(file_exists(ROOT_PATH.'/'.$logo))$str[] = '<link rel="shortcut icon" href="'.$logo.'" />';
		foreach($this->cssarr as $val)$str[] = '<link rel="stylesheet" type="text/css" href="'.$val.'"/>';
		foreach($this->jsarr as $val)$str[]  = '<script type="text/javascript" src="'.$val.'"></script>';
		if($this->style){
			$str[] = '<style type="text/css">';
			foreach($this->style as $val)$str[] = $val;
			$str[] = '</style>';
		}
		if($this->script){
			$str[] = '<script type="text/javascript">';
			foreach($this->script as $val)$str[] = $val;
			$str[] = '</script>';
		}
		$str[] = '</head>';
		$str[] = '<body style="'.$this->bodystyle.'">';
		foreach($this->bodyt as $val)$str[] = $val;
		if($this->viewobj != null){
			$str[] = '';
			echo join(PHP_EOL, $str);
			$str   = array();
			$str[] = $this->viewobj->show();
		}
		if($this->mdivobj)$str[] = $this->mdivobj->show();
		foreach($this->bodyf as $val)$str[] = $val;
		foreach($this->jsarre as $val)$str[]  = '<script type="text/javascript" src="'.$val.'"></script>';
		$str[] = '</body>';
		$str[] = '</html>';
		
		return join(PHP_EOL, $str);
	}
}